/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mobiubiq.directory.client.frame;

import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;
import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.widget.Desktop;
import org.kalmeo.kuix.widget.Widget;
import org.mobiubiq.directory.client.data.Bookmark;
import org.mobiubiq.directory.client.data.BookmarksDataProvider;
import org.mobiubiq.directory.client.data.LocationDataProvider;
import org.mobiubiq.directory.client.object.LocationObject;
import org.mobiubiq.directory.client.widget.GoogleMapsWidget;

/**
 *
 * @author Marnix Renne
 */
public class LocationReader extends DataProviderBaseFrame {

    private LocationObject object;

    public LocationReader() {
        super("read-location.xml", new LocationDataProvider());
    }

    public boolean onMessage(Object identifier, Object[] arguments) {
        if (super.onMessage(identifier, arguments)) {
            if ("detected".equals(identifier)) {
                Location location = (Location) arguments[0];
                object = new LocationObject(location);
                GoogleMapsWidget widget = new GoogleMapsWidget("mapImage", location);
                Desktop desktop = Kuix.getCanvas().getDesktop();
                Widget w = desktop.getWidget("btnBookmark");
                w.setVisible(true);
                w = desktop.getWidget("btnBlog");
                w.setVisible(true);
                return false;
            }
            if ("bookmark".equals(identifier)) {
                if (object != null) {
                    Bookmark bookmark = new Bookmark(object);
                    BookmarksEdit bookmarksEdit = new BookmarksEdit(new BookmarksDataProvider(), bookmark);
                    Kuix.getFrameHandler().pushFrame(bookmarksEdit);
                    return false;
                }
            }
        } else {
            return false;
        }
        return true;
    }

    public void onAdded() {
        super.onAdded();
        new Thread() {

            public void run() {
                try {
                    Criteria criteria = new Criteria();
                    criteria.setHorizontalAccuracy(500);
                    criteria.setVerticalAccuracy(500);
                    LocationProvider locationProvider = LocationProvider.getInstance(criteria);
                    int state = locationProvider.getState();
                    
                    Location location = locationProvider.getLocation(60);
                    if (location != null) {
                        onMessage("detected", new Object[]{location});
                    }
                } catch (InterruptedException ex) {
                    Kuix.alert(ex);
                } catch (LocationException ex) {
                    Kuix.alert(ex);
                }
            }
        }.start();
    }

    /*
    public void locationUpdated(LocationProvider provider, Location location) {
        provider.setLocationListener(null, -1, -1, -1);
        onMessage("detected", new Object[]{location.getQualifiedCoordinates()});

    }

    public void providerStateChanged(LocationProvider provider, int state) {
        if (state == LocationProvider.AVAILABLE) {
            Kuix.alert("available");
        }
        if (state == LocationProvider.OUT_OF_SERVICE) {
            Kuix.alert("out of service");
        }
        if (state == LocationProvider.TEMPORARILY_UNAVAILABLE) {
            Kuix.alert("temporarily unavailable");
        }
    }
     * */
}
